<template>
	<view>
		<u-toast ref="uToast"></u-toast>
		
		<u-navbar
			title="个人设置"
			leftText="返回"
			bgColor="#ffffff"
			@leftClick="leftClick"
			:autoBack="false"
		>
		</u-navbar>
		
		<view style="margin-top: 150px;padding: 20px;">
			<u--form
				labelPosition="left"
				:model="pwd"
				ref="uForm"
			>
				<u-form-item
					label="新密码:"
					prop="onePwd"
					borderBottom
					ref="item1"
					:labelWidth="150"
				>
					<u--input
						v-model="pwd.onePwd"
						border="none"
						placeholder="请输入新密码"
					></u--input>
				</u-form-item>
				
				<u-form-item
					label="确认密码:"
					prop="onePwd"
					borderBottom
					ref="item1"
					:labelWidth="150"
				>
					<u--input
						v-model="pwd.twoPwd"
						border="none"
						placeholder="请再次输入密码"
					></u--input>
				</u-form-item>
				
			</u--form>	
		</view>
		<u-button type="primary" style="margin-top: 50px;" text="确认修改" @click="pwdUpdate" :disabled="pwd.onePwd == '' || pwd.twoPwd == '' ? true :false"></u-button>
	</view>
</template>

<script>
import { updateUser } from '../../../utils/user';
	export default{
		data(){
			return{
				userId: undefined,
				pwd: {
					onePwd: '',
					twoPwd: '',
				}
			}
		},
		methods:{
			leftClick(){
				// #ifdef H5
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}
				//使用vue-router返回上一级
				let a = this.$router.go(-1)
				if (a == undefined) {
					//重新定向跳转页面
					uni.reLaunch({
						url: '/pages/view/homepage/homepage'    
					})   
				}
				return;
				// #endif
				uni.navigateBack(1)
			},
			pwdUpdate(){
				if(this.pwd.onePwd == this.pwd.twoPwd){
					let userUpdateRequest = {
						id: this.userId,
						userPassword: this.pwd.onePwd
					}
					updateUser(userUpdateRequest).then(res=>{
						if(res.code == 0){
							console.log(res)
							this.$refs.uToast.show({
								type: 'success',
								title: '修改成功,请重新登录',
								message: "修改成功,请重新登录",
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
							})
							setTimeout(()=>{
								uni.reLaunch({
								 url: "/pages/index/index"
								})
							},2000)
							
						}
					})
				}else{
					this.$refs.uToast.show({
						type: 'error',
						title: '密码不一致',
						message: "密码不一致",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
					})
					this.pwd.onePwd = ''
					this.pwd.twoPwd = ''
				}
			}
		},
		onLoad: function(option) {
			this.userId = option.id
		}
		
	}
</script>

<style scoped>
	
</style>